/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trefliq.EJB.sessionbeans;

import java.util.Date;
import java.util.List;
import javax.annotation.security.PermitAll;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import trefliq.EJB.entities.Inventory;

/**
 *
 * @author dsimmons
 */
@Stateless
@LocalBean
@PermitAll // (by default, allow all, restrict what's necessary)
public class InventorySession {
    
    @PersistenceContext(unitName="trefliqPU")
    private EntityManager em;

    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN})
    public Inventory createInventory(Inventory Inventory) {
        em.persist(Inventory);
        return Inventory;
    }
    
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN})
    public List<Inventory> getAllInventory() {
        return em.createNamedQuery("findAllInventory", Inventory.class).getResultList();
    }
    
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public Inventory getInventoryById(Long InventoryId) {
        return em.createNamedQuery("findInventoryById", Inventory.class).
                setParameter("id", InventoryId).getSingleResult();
    }
    
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public Inventory getInventoryForMediaId(Long mediaId) {
        return em.createNamedQuery("findInventoryByMediaId", Inventory.class).
                setParameter("media", mediaId).getSingleResult();
    }
    
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
    public Inventory getInventoryForMovie(Long movieId) {
        return em.createNamedQuery("findInventoryByMovie", Inventory.class).
                setParameter("movie", movieId).getSingleResult();
    }    
}
